﻿
function registerKindeditor(id,width,configuration) {
	KE.lang['code'] = "插入程序代码或脚本";
	KE.plugin['code'] = {
		click: function (id) {
			KE.util.selection(id);
			var dialog = new KE.dialog({
				id: id,
				cmd: 'code',
				file: './code/code.html',
				width: 520,
				height: 280,
				title: KE.lang['code'],
				yesButton: KE.lang['yes'],
				noButton: KE.lang['no']
			});
			dialog.show();
		},
		check: function (id) {
			var dialogDoc = KE.util.getIframeDoc(KE.g[id].dialog);
			var lang = KE.$('ic_lang', dialogDoc).value;
			var source = KE.$('ic_source', dialogDoc).value;
			if (lang == '') {
				alert('编程语言必须选择');
				return false;
			}
			if (source == '') {
				alert('请输入程序代码或者脚本');
				return false;
			}
			return true;
		},
		exec: function (id) {
			KE.util.select(id);
			var iframeDoc = KE.g[id].iframeDoc;
			var dialogDoc = KE.util.getIframeDoc(KE.g[id].dialog);
			if (!this.check(id)) return false;
			var lang = KE.$('ic_lang', dialogDoc).value;
			var source = KE.$('ic_source', dialogDoc).value;
			this.insert(id, lang, source);
		},
		insert: function (id, lang, source) {
			var html = '<pre class="brush:' + lang + '; toolbar: false; auto-links: false;">';
			html += source;
			html += '</pre>';
			KE.util.insertHtml(id, html);
			KE.layout.hide(id);
			KE.util.focus(id);
		}
	};
	var items = null;

	if (configuration == undefined||configuration==null)
		configuration=0;
	switch(configuration){
		case 0:items=['code', '|',
				'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
				'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
				'superscript', '|', 'title', 'fontname', 'fontsize', '|', 'textcolor', 'bgcolor', 'bold',
				'italic', 'underline', 'strikethrough', '|', 'image',
				'flash', 'advtable', 'hr', 'emoticons', 'link', 'unlink','|', 'fullscreen' ];
				break;
			case 1: items = ['code', '|',
				 'justifyleft', 'justifycenter', 'justifyright',
				'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
				'superscript', '|', 'title', 'fontname', 'fontsize', '|', 'textcolor', 'bgcolor', 'bold',
				'italic', 'underline', 'strikethrough', '|', 
				 'hr',  'link', 'unlink', '|', 'fullscreen'];
				break;
	}
	KE.show({
		id: id,
		width: width == undefined||width==null ? "100%" : width,
		resizeMode: 2,
		shadowMode: false,
		allowPreviewEmoticons: false,
		allowUpload: true,
		imageUploadJson: '/upload/Picture/',
		urlType: 'absolute',
		newlineTag: 'br',
		cssPath: '/Scripts/KindEditor/plugins/code/code.css',
		items:items
	});
}

/* recommend article */
function recommend(url, id, recommendTimesID, serial, cookieName, tip) {

	if ($.cookie(cookieName) != null) {
		var cookieValue = $.cookie(cookieName);
		var blogIDs = cookieValue.split('#');
		for (var i = 0; i < blogIDs.length; i++) {
			if (blogIDs[i] == id) {
				alert(tip);
				return;
			}
		}
	}
	$.ajax({
		type: "POST",
		url: url,
		data: "id=" + id,
		success: function (msg) {
			if (msg == "") {
				var times = parseInt($("#" + recommendTimesID + serial).html()) + 1;

				$("#" + recommendTimesID + serial).html(times);
				$("#" + recommendTimesID + serial).parent().fadeOut();
				$("#" + recommendTimesID + serial).parent().fadeIn('slow');
				//set cookie
				if ($.cookie(cookieName) == null) {
					$.cookie(cookieName, id, { expires: 7 });
				} else {
					var cookieValue = $.cookie(cookieName) + "#" + id;
					$.cookie(cookieName, cookieValue);
					$.cookie(cookieName, cookieValue, { expires: 7 });
				}
				return;
			}
		}
	});
}